package com.mq.receiver;

import com.mq.entity.User;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author xwj
 * @date 2019/7/23
 */
@Component
public class DelayReceiver {

    @RabbitListener(queues = {"delay_queue_5s"})
    public void processDelayQueue5s1(User user, Message message, Channel channel) throws IOException {
        System.out.println("============消费5秒延时队列==============");
        System.out.println("消费时间：" + LocalDateTime.now());
        System.out.println("交换机：" + message.getMessageProperties().getReceivedExchange());
        System.out.println("routing key：" + message.getMessageProperties().getReceivedRoutingKey());
        System.out.println("队列：" + message.getMessageProperties().getConsumerQueue());
        System.out.println("消息内容：" + user.toString());
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, true);
        System.out.println("=======================================");
    }

//    @RabbitListener(queues = {"delay_queue_5s"})
//    public void processDelayQueue5s2(User user, Message message, Channel channel) throws IOException {
//        System.out.println("============消费5秒延时队列2==============");
//        System.out.println("交换机：" + message.getMessageProperties().getReceivedExchange());
//        System.out.println("routing key：" + message.getMessageProperties().getReceivedRoutingKey());
//        System.out.println("队列：" + message.getMessageProperties().getConsumerQueue());
//        System.out.println("消息内容：" + user.toString());
//        System.out.println("=======================================");
//    }

    @RabbitListener(queues = {"delay_queue_10s"})
    public void processDelayQueue10s(User user, Message message, Channel channel) throws IOException {
        System.out.println("============消费10秒延时队列==============");
        System.out.println("交换机：" + message.getMessageProperties().getReceivedExchange());
        System.out.println("routing key：" + message.getMessageProperties().getReceivedRoutingKey());
        System.out.println("队列：" + message.getMessageProperties().getConsumerQueue());
        System.out.println("消息内容：" + user.toString());
        System.out.println("=======================================");
    }

    @RabbitListener(queues = {"delay_queue_30s"})
    public void processDelayQueue30s(User user, Message message, Channel channel) throws IOException {
        System.out.println("============消费30秒延时队列==============");
        System.out.println("交换机：" + message.getMessageProperties().getReceivedExchange());
        System.out.println("routing key：" + message.getMessageProperties().getReceivedRoutingKey());
        System.out.println("队列：" + message.getMessageProperties().getConsumerQueue());
        System.out.println("消息内容：" + user.toString());
        System.out.println("=======================================");
    }

    @RabbitListener(queues = {"delay_queue_60s"})
    public void processDelayQueue60s(User user, Message message, Channel channel) throws IOException {
        System.out.println("============消费60秒延时队列==============");
        System.out.println("交换机：" + message.getMessageProperties().getReceivedExchange());
        System.out.println("routing key：" + message.getMessageProperties().getReceivedRoutingKey());
        System.out.println("队列：" + message.getMessageProperties().getConsumerQueue());
        System.out.println("消息内容：" + user.toString());
        System.out.println("=======================================");
    }

}
